package com.scpii.api.controller.user;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.scpii.api.domain.user.UserFeedBack;
import com.scpii.api.service.user.UserFeedBackService;
import com.scpii.api.util.FileUtil;
import com.scpii.api.util.PrincipalUtil;

@Controller
@RequestMapping("/user/feedBack")
public class FeedBackController {

	@Resource
	private UserFeedBackService userFeedBackService;

	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public Boolean addFeedBack(
			@RequestParam(value = "content", required = false) String content,
			@RequestParam(value = "log", required = false) MultipartFile log,
			@RequestParam("config") MultipartFile config,
			@RequestParam("appVersion") String appVersion) {
		Long userId = PrincipalUtil.getPrincipal().getId();
		Long appId = PrincipalUtil.getPrincipal().getAppId();
		Long deviceId = PrincipalUtil.getPrincipal().getDeviceId();

		UserFeedBack feedBack = new UserFeedBack();
		feedBack.setContent(content);
		feedBack.setLog(FileUtil.readFileToString(log));
		feedBack.setConfig(FileUtil.readFileToString(config));
		feedBack.setAppVersion(appVersion);
		feedBack.setCreateBy(userId);
		feedBack.setUserId(userId);
		feedBack.setCreateDt(new Date());
		feedBack.setDeviceId(deviceId);
		feedBack.setAppId(appId);
		userFeedBackService.insertSelective(feedBack);
		return true;
	}
}
